<?php
// +----------------------------------------------------------------------
// | 幕小铺
// +----------------------------------------------------------------------
// | 版权所有 2016~2020 幕小铺科技（大连）有限公司
// +----------------------------------------------------------------------
// | 2020/03/14
// +----------------------------------------------------------------------

namespace app\video\model;

use think\Model;

/**
 * 视频举报模型
 * @package app\video\model
 */
class VideoPlayReport extends Model
{
    // 设置当前模型对应的完整数据表名称
    protected $table = 'xm_video_play_report';

    public static function init(){
        self::event('after_insert', function ($user) {
             //视频表 举报增加
            model('VideoPlay') -> incReportTimes($user->play_id);
        });
        self::event('after_delete', function ($user) {
			//视频表 举报-1
            model('VideoPlay') -> decReportTimes($user->play_id);
        });
    }

    public function getReportStatusAttr($value){
        $status = [0=>'待审核',1=>'有效举报',2=>'无效举报'];
        return $status[$value];
    }
	
	/**
     * 添加数据
     * @param  int 	  $user_id     用户id
     * @param  int 	  $play_id     视频id
     * @param  int 	  $r_uid       被举报用户uid
     * @param  int 	  $reason      举报理由id
     * @param  string $describe    举报详情
     * @param  string $img_url_1   图片1
     * @param  string $img_url_2   图片2
     * @param  string $img_url_3   图片3
     * @param  string $img_url_4   图片4
     * @author shiqiang <976162041@qq.com>
     * @return bool
     */
	public function addData($user_id, $report_type, $play_id, $comment_id, $reply_id, $r_uid, $reason, $describe, $img_url_1, $img_url_2, $img_url_3, $img_url_4){
		$video_info = model('video/VideoPlay') -> getPlayFatherPlayInfo($play_id);
		$play_id = $video_info['data'];//关联的视频id	
		
        $this->uid 			= $user_id;
        $this->report_type  = $report_type;
        $this->play_id 		= $play_id;
        $this->comment_id   = $comment_id;
        $this->reply_id     = $reply_id;
		$this->r_uid 		= $r_uid;
		$this->reason 		= $reason;
		$this->describe 	= $describe;
		$this->img_url_1 	= $img_url_1;
		$this->img_url_2 	= $img_url_2;
		$this->img_url_3 	= $img_url_3;
		$this->img_url_4 	= $img_url_4;
        $res = $this -> isUpdate(false) -> save();
        if($res){
            return array_out(200, 'success');
        }else{
            return array_out(-1001, 'error');
        }
	}
	
	/**
     * 删除数据
     * @param int $uid 			用户id
	 * @param int $play_id      视频id
     * @param int $report_id 	举报id
     * @author shiqiang <976162041@qq.com>
     * @return bool
     */
    public function deleteData($uid, $play_id, $report_id){
        $map        = [];
        $map['uid']     	= $uid;
        $map['play_id']     = $play_id;
        $map['report_id'] 	= $report_id;
        $res = self::destroy(function($query) use ($map){
            $query->where($map);
        });
        if($res){
            return array_out(200, 'success');
        }else{
            return array_out(-1001, 'error');
        }
    }

    /**
     * 获取用户点赞 的列表
     * @param int $uid 用户id
     * @param int $page 页码（默认1）
     * @author shiqiang <976162041@qq.com>
     * @return array
     */
    public function getListByUid($uid, $page = 1){
        
		$map = [];
        $map['video_play_support.uid'] 	= $uid;
        $map['getVideoPlay.play_status'] 			= 1;//视频状态 0禁用 1启用 2测试可见
        $map['getVideoPlay.play_operation_status'] 	= 1;//操作状态 0 待审 1 正常 2拒绝
        $list = self::withJoin('getVideoPlay,getUcMember,getUcMemberCount', 'LEFT') -> where($map) -> order('create_time' ,'desc') -> paginate();
        return array_out(200, 'success', $list);
    }

    /**
     * 用户信息 关联载入（一对一）
     */
    public function getUcMember(){
        return $this -> hasOne('app\user_center\model\user\Member','uid', 'uid');
    }

    /**
     * 视频表 关联载入（一对一）
     */
    public function getVideoPlay(){
        return $this -> hasOne('app\video\model\VideoPlay','id', 'play_id');
    }
}
